#!/usr/bin/env python3
import yaml
import os
import shutil
import sys
from pathlib import Path

def copy_scene_info(yaml_file):
    with open(yaml_file, 'r') as f:
        data = yaml.safe_load(f)
    
    paths = data.get('paths', {})
    
    for group_name, group_data in paths.items():
        if not isinstance(group_data, dict):
            continue
            
        keys = list(group_data.keys())
        keys.sort()
        print(keys)
        if len(keys) < 8:
            continue
        key_3 = keys[3]
        key_4 = keys[4] 
        key_7 = keys[7]
        if not (str(key_3).endswith('3') and str(key_4).endswith('4') and str(key_7).endswith('7')):
            continue
            
        entry_3 = group_data.get(key_3, {})
        entry_4 = group_data.get(key_4, {})
        entry_7 = group_data.get(key_7, {})
        if '463' in keys:
            print("entry 3",keys,entry_3)
        if not entry_3 or not entry_4 or not entry_7:
            print(f"Missing entry: {key_3}, {key_4}, {key_7}")
            continue
            
        source_path = entry_3.get('name')
        if not source_path:
            continue
            
        source_scene_info = os.path.join(source_path, 'scene_info.json')
        if not os.path.exists(source_scene_info):
            continue
            
        dest_path_4 = entry_4.get('name')
        dest_path_7 = entry_7.get('name')
        
        if dest_path_4 and os.path.exists(dest_path_4):
            dest_scene_info_4 = os.path.join(dest_path_4, 'scene_info.json')
            try:
                shutil.copy2(source_scene_info, dest_scene_info_4)
                print(f"Copy success: {source_scene_info} -> {dest_scene_info_4}")
            except Exception as e:
                print(f"Copy failed {source_scene_info} -> {dest_scene_info_4}: {e}")
        
        if dest_path_7 and os.path.exists(dest_path_7):
            dest_scene_info_7 = os.path.join(dest_path_7, 'scene_info.json')
            try:
                shutil.copy2(source_scene_info, dest_scene_info_7)
                print(f"Copy success: {source_scene_info} -> {dest_scene_info_7}")
            except Exception as e:
                print(f"Copy failed {source_scene_info} -> {dest_scene_info_7}: {e}")

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: python copy_scene_info.py <yaml_file>")
        sys.exit(1)
    
    yaml_file = sys.argv[1]
    if not os.path.exists(yaml_file):
        print(f"File not found: {yaml_file}")
        sys.exit(1)
    
    copy_scene_info(yaml_file)
